import request from '/@/utils/request';

/**
 * Jenkins凭证管理API接口
 */

// 凭证项接口定义
export interface CredentialItem {
	id: number;
	credentialId: string;
	description: string;
	type: 'usernamePassword' | 'sshKey' | 'secretText' | 'certificate';
	username?: string;
	scope: 'GLOBAL' | 'SYSTEM';
	createdTime: string;
	updatedTime: string;
	usageCount: number;
	creator?: string;
	modifier?: string;
}

// 凭证表单数据接口
export interface CredentialFormData {
	id?: number;
	credentialId: string;
	description: string;
	type: string;
	username?: string;
	password?: string;
	privateKey?: string;
	passphrase?: string;
	secretText?: string;
	certificate?: string;
	scope: string;
}

// 分页查询参数接口
export interface CredentialPageParams {
	page: number;
	size: number;
	keyword?: string;
	type?: string;
}

// 分页响应接口
export interface CredentialPageResponse {
	records: CredentialItem[];
	total: number;
	page: number;
	size: number;
}

/**
 * Jenkins凭证API集合
 */
export function credentialApi() {
	return {
		// 获取凭证列表（分页）
		pageCredentials: (data: CredentialPageParams) => {
			return request({
				url: '/jenkins/credentials/list',
				method: 'post',
				data,
			});
		},

		// 创建凭证
		createCredential: (data: CredentialFormData) => {
			return request({
				url: '/jenkins/credentials/create',
				method: 'post',
				data,
			});
		},

		// 更新凭证
		updateCredential: (data: CredentialFormData) => {
			return request({
				url: '/jenkins/credentials/update',
				method: 'put',
				data,
			});
		},

		// 删除凭证
		deleteCredential: (params: { id: number }) => {
			return request({
				url: '/jenkins/credentials/delete',
				method: 'delete',
				data: params,
			});
		},

		// 批量删除凭证
		batchDeleteCredentials: (data: { ids: number[] }) => {
			return request({
				url: '/jenkins/credentials/batch-delete',
				method: 'delete',
				data,
			});
		},

		// 获取凭证详情
		getCredentialDetail: (params: { id: number }) => {
			return request({
				url: '/jenkins/credentials/detail',
				method: 'get',
				params,
			});
		},

		// 获取凭证选项
		getCredentialOptions: () => {
			return request({
				url: '/jenkins/credentials/options',
				method: 'get',
			});
		},
	};
}